package com.hqyj.controller;

import com.hqyj.anno.UserActionType;
import com.hqyj.dao.CourceMapper;
import com.hqyj.dao.KaoQinMapper;
import com.hqyj.dao.StudentMapper;
import com.hqyj.pojo.KaoQin;
import com.hqyj.pojo.Student;
import com.hqyj.service.KaoQinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Controller
@RequestMapping("kaoqin")
public class KaoQinController {

    @Autowired
    KaoQinService kaoQinService;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    CourceMapper courceMapper;

    @RequestMapping("kaoqinPage")
    public String kaoqinPage() {
        return "attendance/attendanceList";
    }


    //查询
    @UserActionType(Ltype = "进入考勤界面",desc = "执行查询所有记录操作")
    @RequestMapping("selectAll")
    @ResponseBody
    public HashMap<String, Object> selectAll(KaoQin kaoQin) {
        return kaoQinService.selectAll(kaoQin);
    }


    //修改
    @UserActionType(Ltype = "进入考勤界面",desc = "执行修改记录操作")
    @RequestMapping(value = "update")
    @ResponseBody
    public HashMap<String, Object> updateByKid(KaoQin kaoQin) {
        System.out.println("接受传值:" + kaoQin);
        //格式化时间
        String time1 = kaoQin.getStudent().getsSex();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time1);
            kaoQin.setqTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return kaoQinService.updateByPrimaryKeySelective(kaoQin);
    }

    //查询
    @RequestMapping("selectAllCource")
    @ResponseBody
    public HashMap<String, Object> selectAllCource() {

        HashMap<String, Object> map = new HashMap<>();
        map.put("cource", courceMapper.selectAll());
        map.put("student", studentMapper.selectAll());
        System.err.println(map);

        return map;
    }


    //删除
    @UserActionType(Ltype = "进入考勤界面",desc = "执行删除记录操作")
    @RequestMapping("delete")
    @ResponseBody
    public HashMap<String, Object> delete(KaoQin kaoQin) {
        return kaoQinService.deleteByPrimaryKey(kaoQin.getkId());
    }


    //添加

    @UserActionType(Ltype = "进入考勤界面",desc = "执行添加记录操作")
    @RequestMapping("add")
    @ResponseBody
    public HashMap<String, Object> add(KaoQin kaoQin) {
        System.out.println("新增接受传值:" + kaoQin);
        //格式化时间
        String time1 = kaoQin.getStudent().getsSex();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time1);
            kaoQin.setqTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("kaoqinzhi" + kaoQin);
        return kaoQinService.insertSelective(kaoQin);
    }


    //模糊查询
    @RequestMapping("selectByName")
    @ResponseBody
    public HashMap<String, Object> selectByName(Student student) {

        String sName = student.getsName();
        System.out.println("接受名字--------------------"+sName);


        return kaoQinService.selectAllByName(student);
    }
}
